import logging
from django.contrib.auth.backends import ModelBackend
from erp.erp_system.models.user import UserModel

logger = logging.getLogger('erp')


class UserLoginAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        """
        实现用户认证
        :param request:
        :param username:
        :param password:
        :param kwargs:
        :return:
        """
        try:
            user = UserModel.objects.get(username=username)
            logger.info(user)
        except:
            return None
        # 判断密码
        if user.check_password(password):
            return user  # 把user对象放到request对象中
